package handler

type Solution interface {
	Arithmetic() interface{}
	Arithmetic2() interface{}
}

type ValidBracket struct {
	Str string
}

func (a ValidBracket) Arithmetic() interface{} {
	s := a.Str
	n := len(s)
	if n%2 == 1 {
		return false
	}

	pairs := map[byte]byte{
		'}': '{',
		']': '[',
		')': '(',
	}

	stack := []byte{}
	for i := 0; i < n; i++ {
		if pairs[s[i]] != 0 {
			if len(stack) == 0 || pairs[s[i]] != stack[len(stack)-1] {
				return false
			}
			stack = stack[:len(stack)-1]
		} else {
			stack = append(stack, s[i])
		}
	}

	return len(stack) == 0
}

type ListNode struct {
	Val  int
	Next *ListNode
}

type ListReversal struct {
	Node *ListNode
}

func (a ListReversal) Arithmetic() interface{} {
	curr := a.Node
	if curr == nil || curr.Next == nil {
		return curr
	}
	pre := &ListNode{}
	for curr.Next != nil {
		next := curr.Next
		curr.Next = pre
		pre = curr
		curr = next
	}
	return pre
}

func (a ListReversal) Arithmetic2() interface{} {
	curr := a.Node
	if curr == nil || curr.Next == nil {
		return curr
	}
	return reversal(curr)
}

func reversal(node *ListNode) *ListNode {
	if node == nil || node.Next == nil {
		return node
	}
	newNode := reversal(node.Next)
	node.Next.Next = node
	node.Next = &ListNode{}
	return newNode
}
